<?php
/*
   Copyright 2002 - 2005 Sean Proctor, Nathan Poiro

   This file is part of PHP-Calendar.

   PHP-Calendar is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   PHP-Calendar is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with PHP-Calendar; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

if ( !defined('IN_PHPC') ) {
       die("Hacking attempt");
}

function show_available()
{
	global $config, $phpc_script, $vars, $calendar_name, $noNavbar;

	if (!isset( $_SESSION['uid'])){
		redirect($phpc_script . "?action=login");
	}
	
	$noNavbar = true;
	
	
	//TODO validate starttime and endtime
	if ($vars['starttime']){
		$starttimeDetail = strtotime($vars['starttime']);
	}
	
	if ($vars['endtime']){
		$endtimeDetail = strtotime($vars['endtime']);
	}		
  $fifteenMinutes = 60*15;

	//TODO Oops Red lines are being rounded to hours not to 15 mins
	//force start time and end time to be on 15 minute increments	
	$starttime = strtotime(date('Y-m-d H:00:00', $starttimeDetail));
	$endtime = strtotime(date('Y-m-d H:00:00', $endtimeDetail));
	
	$starttimeDetail = floor(($starttimeDetail - $starttime) / $fifteenMinutes) * $fifteenMinutes + $starttime;
	$endtimeDetail   = floor(($endtimeDetail   - $endtime) / $fifteenMinutes) *   $fifteenMinutes + $endtime;
	
	$interval = ($endtime - $starttime) / ($fifteenMinutes);
	if ($interval < 4){
		$interval = 4;
		$endtime = $starttime + $interval;
	}
	
	$step = ceil(($interval/4)/20); //convert the interval to hours and divide by twenty
	
	$starttimePadded = $starttime - ceil($interval * .025) * $fifteenMinutes * 4;
	$endtimePadded = $endtime + ceil($interval * .025) * $fifteenMinutes * 4;	
	
	$tbl = tag("table", attributes("border='0' cellpadding='0' cellspacing='0'"));

  //days legend
	$tr = tag("tr");
	$tr->add(tag("td", attributes("nowrap='true'"), ""));
	if ( 24 - date('H', $starttimePadded) > 0 ){
		$tr->add(tag("td", attributes("colspan='" . (24 - date('H', $starttimePadded))*4 . "'"), ""));
	}
	
	$style='';
	for ($i = $starttimePadded; $i < $endtimePadded; $i += $fifteenMinutes){
		  $style = '';
			if (date('H:i', $i) == "00:00"){
				$cont = date('D j', $i);
				$style .= " border-left: 1px solid #000000; ";
		    $tr->add(tag("td", attributes("colspan='" . (4*24) . "' nowrap='true' style='$style'"), $cont));
			}
	}
	$tbl->add($tr);
	
  //hours legend
	$tr = tag("tr");
	$tr->add(tag("td", attributes("nowrap='true'"), ""));
	for ($i = $starttimePadded; $i < $endtimePadded; $i +=  ($fifteenMinutes * 4 * $step)){
		if(date('i', $i) != 0) echo date('i', $i);
		$cont = date('ga', $i);
		$style = " border-left: 1px solid #000000; ";
		$tr->add(tag("td", attributes("class='sa' colspan='" . 4 * $step . "' nowrap='true' style='$style'"), $cont));
	}
	$tbl->add($tr);
	
	$result = get_vehicles($calendar_name);
	if ($row1 = $result->FetchRow()) {
		for (; $row1; $row1 = $result->FetchRow()) {
					$tr = tag("tr");
					$tr->add(tag("td", attributes("class='sa' style='background-color: ".$row1['color']."' nowrap='true'"), $row1['dropdownname']));
					
					$eventsRows = get_events_for_interval($starttimePadded, $endtimePadded, $row1['id']);
					
					$events = $eventsRows->GetRows();
					
					for ($i = $starttimePadded; $i < $endtimePadded; $i +=  $fifteenMinutes){
						$cont = "<img src='/images/spacer.gif' width='2' height='1'/>";
						$bgcolor = "#FFFFFF;";
						foreach($events as $row) {
							$stStamp = strtotime($row['starttime']);
							$endStamp = strtotime($row['endtime']);
							$curDayStart = $i;
							$curDayEnd = $i + $fifteenMinutes;
							if ( $curDayEnd > $stStamp && $curDayStart < $endStamp ){
								$bgcolor =  $row1['color'] . ";";
							} 
						}	
						$style = " background-color: $bgcolor";
						if ( $i == $starttimeDetail || $i == $endtimeDetail){
							$style .= " border-left: 1px solid #EE0000; ";
						} else if ( date('i', $i) == "00" ){
							$style .= " border-left: 1px dotted #000000; ";
						}	else {
							
						}					
						$tr->add(tag("td", attributes("class='sa' nowrap='true' style='$style'"), $cont));
				}
				$tr->add(tag("td", attributes("class='sa' style='background-color: ".$row1['color']."' nowrap='true'"), $row1['dropdownname']));
				$tbl->add($tr);
		}
		
	}
		

	
	$outTxt = tag("div", attributes("align='center'", 'style="width: 90%"'));
	$outTxt->add($tbl);
	$outTxt->add(tag("br"));
	$outTxt->add(tag('p', attributes("align='center'"), tag("a",attributes("href='#' onClick='window.close();'"),"close")));
  return $outTxt;
}

?>
